package com.youhome.business.controller;

import com.youhome.business.dto.DTOMessageSend;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.parameter.ParamMessageSend;
import com.youhome.service.service.BMessageService;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/12/4.
 */
@RestController
@RequestMapping("/message")
@Api(tags = "站内信")
public class MessageController {
    @Autowired
    private BMessageService bMessageService;


    @ApiOperation(value = "发送站内信息", notes = "发送站内信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/message/send")
    public Object messageSend(@ApiParam @Validated DTOMessageSend dtoMessageSend,
                              @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        bMessageService.send(principal,modelMapper.map(dtoMessageSend, ParamMessageSend.class));
        return new ResultPage(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "站内信息列表", notes = "站内信息列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "是否已读，0未读，1已读", paramType = "query")
    })
    @GetMapping(value = "/message/list")
    public Object messageList(Integer pageNumber, Integer pageSize, Integer status,
                              @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bMessageService.selectByBean(principal,status, pageNumber, pageSize));
    }


    @ApiOperation(value = "站内信息列表发送历史记录", notes = "站内信息列表发送历史记录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "是否已读，0未读，1已读", paramType = "query")
    })
    @GetMapping(value = "/message/history/list")
    public Object messageHistoryList(Integer pageNumber, Integer pageSize, Integer status,
                                     @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bMessageService.selectByBean(principal,status, pageNumber, pageSize));
    }

    @ApiOperation(value = "站内信息数量", notes = "站内信息数量", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "status", value = "信息状态，0:未读，1:已读", paramType = "query")
    })
    @GetMapping(value = "/message/count")
    public Object messageCount(Integer status,
                               @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bMessageService.selectCount(principal,status));
    }


    @ApiOperation(value = "站内信息详情", notes = "站内信息详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "id", value = "信息ID", required = true, paramType = "query")
    })
    @GetMapping(value = "/message/details")
    public Object messageDetails(Long id) throws CustomException {
        return new Result(ResultCode.SUCCESS, bMessageService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "删除站内信息", notes = "删除站内信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "id", value = "信息ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "类型 0：发送者，1：接收者", required = true, paramType = "query")
    })
    @PutMapping(value = "/message/delete")
    public Object messageDelete(Long id, Integer type,
                                @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bMessageService.delete(principal,id, type);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "修改站内信息状态", notes = "修改站内信息状态", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "id", value = "信息ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "状态0:未读，1:已读", required = true, paramType = "query")
    })
    @PutMapping(value = "/message/update/status")
    public Object updateStatus(Long id, Integer status,
                               @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bMessageService.updateStatus(principal,id, status);
        return new Result(ResultCode.SUCCESS);
    }


}
